<?php
include_once 'admin_global.php';            //加载后台统一调用类文件
$db->Get_user_shell_check($uid, $shell);    //检测用户是否登录

//查询数据库，获取要修改的用户信息，并且把相关信息显示在表单中
$userID=$_GET['userID'];
$query=$db->select("admin","*","userID=$userID");
$result=$db->fetch_array();
//先判断管理员用户类型,决定其是否具有修改管理员类型的权限
if($_SESSION['userType']=='普通管理员'){
    $userTypeSelect="disabled=true";
    $isEnabledSelect="disabled=true";
}else{
    $userTypeSelect="";
    $isEnabledSelect="";
}
//准备修改用户信息
if($_POST['editAdmin']){
    //判断是否输入了新密码
    $newPassword=trim($_POST['newPassword']);
    if(!empty($newPassword)){
        //输入了新密码，修改的时候要修改密码
        //按照管理员类型区分，超级管理员直接修改，普通管理员只能修改自己的密码，且需要输入原始密码
        if($_SESSION['userType']=='超级管理员'){
            $adminPassword=sha1(trim($_POST['userPassword']));
            $query = $db->select('admin', "*", "userID=$_SESSION[uid]");
            $result=$db->fetch_array();
            if($adminPassword==$result['userPassword']){
                $newPassword=sha1($newPassword);
                $userEmail=trim($_POST['userEmail']);
                $userType=$_POST['userType'];
                $userSex=$_POST['userSex'];
                if(!empty($_POST['userEnabled'])){
                    $userEnabled=1;
                }else{
                    $userEnabled=0;
                }
                $userRemark=trim($_POST['userRemark']);
                $rows=$db->update("admin","userPassword='$newPassword',userEmail='$userEmail',userType='$userType',userSex='$userSex',userEnabled=$userEnabled,userRemark='$userRemark'","userID=$userID");
                if($rows==1){
                    echo "<script>alert('修改成功！');location.href='listAdmin.php';</script>"; 
                }else{
                    echo "<script>alert('修改失败！');location.href='listAdmin.php';</script>";
                }
            }else{
               echo "<script>alert('管理员密码错误！');history.go(-1);</script>";
            }
        }else{
            $adminPassword=sha1(trim($_POST['userPassword']));
            if($adminPassword==$result['userPassword']){
                $newPassword=sha1($newPassword);
                $userEmail=trim($_POST['userEmail']);
                $userSex=$_POST['userSex'];
                $userRemark=trim($_POST['userRemark']);
                $rows=$db->update("admin","userPassword='$newPassword',userEmail='$userEmail',userSex='$userSex',userRemark='$userRemark'","userID=$userID");
                if($rows==1){
                    echo "<script>alert('修改成功！');location.href='listAdmin.php';</script>"; 
                }else{
                    echo "<script>alert('修改失败！');location.href='listAdmin.php';</script>";
                }
            }else{
                echo "<script>alert('原始密码错误！');history.go(-1);</script>"; 
            }
        }
    }else{
        //没有输入新密码，修改的时候不用修改密码
        if($_SESSION['userType']=='超级管理员'){
            $userEmail=trim($_POST['userEmail']);
            $userType=$_POST['userType'];
            $userSex=$_POST['userSex'];
            if(!empty($_POST['userEnabled'])){
                $userEnabled=1;
            }else{
                $userEnabled=0;
            }
            $userRemark=trim($_POST['userRemark']);
            $rows=$db->update("admin","userEmail='$userEmail',userType='$userType',userSex='$userSex',userEnabled=$userEnabled,userRemark='$userRemark'","userID=$userID");
            if($rows==1){
                echo "<script>alert('修改成功！');location.href='listAdmin.php';</script>"; 
            }else{
                echo "<script>alert('修改失败！');location.href='listAdmin.php';</script>";
            }
        }else{
            $userEmail=trim($_POST['userEmail']);
            $userSex=$_POST['userSex'];
            $userRemark=trim($_POST['userRemark']);
            $rows=$db->update("admin","userEmail='$userEmail',userSex='$userSex',userRemark='$userRemark'","userID=$userID");
            if($rows==1){
                echo "<script>alert('修改成功！');location.href='listAdmin.php';</script>"; 
            }else{
                echo "<script>alert('修改失败！');location.href='listAdmin.php';</script>";
            }
        }
    }
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>编辑管理员</title>
        <link rel="stylesheet" href="css/right.css" type="text/css" /> 
    </head>
    <script>
        function focusInput(focusClass, normalClass) {
            var elements = document.getElementsByTagName("input");
            for (var i=0; i < elements.length; i++) {
                if (elements[i].type != "button" && elements[i].type != "submit" && elements[i].type != "reset") {
                    elements[i].onfocus = function() { this.className = focusClass; };
                    //elements[i].onblur = function() { this.className = normalClass||''; };
                }
            }
        }
        window.onload=function(){
            focusInput('int_text', 'int_text1');

            var myform=document.getElementById("form1");
            myform.onsubmit=check_editUser;
            function check_editUser(){
                if(myform.newPassword.value!=''){
                    if(myform.userPassword.value==""){
                        alert("请输入管理员密码");
                        myform.userPassword.focus();
                        return false;
                    }
                    if(myform.RenewPassword.value!=myform.newPassword.value){
                        alert("两次输入的密码不一致！");
                        myform.ReuserPassword.focus();
                        return false;
                    }
                }
                var email=myform.userEmail.value;
                if(email!=""){
                    var re=/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/;
                    if(re.test(email)!=true){
                        alert("电子邮件格式不符合要求！");
                        return false;
                    }
                }
            }
        }   
    </script>
    <body>
        <div id="right_main">
            <div class="right_content">
                <div class="right_top_menu">
                    <h3>
                        您当前的位置：后台 > 管理设置 > 编辑管理员
                    </h3>
                </div>
                <div class="right_center_content">
                    <form id="form1" method="post" action="">
                        <table cellpadding="0" cellspacing="0" border="0" width="100%">
                            <tr>
                                <td valign="top" >
                                    <div class="txl_search"></div>
                                    <table cellpadding="0" cellspacing="0" border="0" width="100%"  class="sy_table">
                                        <tr>
                                            <th colspan="2" >编辑管理员</th>
                                        </tr>
                                        <tr>
                                            <td width="35%" align="right">管理员名称：</td>
                                            <td width="65%"><input type="text" name="userName"  class="int_text1" value="<?php echo $result['userName']; ?>" size="40"  disabled=true> </td>
                                        </tr>
                                        <tr>
                                                <td width="35%" align="right">管理员密码：</td>
                                                <td width="65%"><input type="password" name="userPassword"  class="int_text1" value="" size="40" />普通管理员请输入原始密码</td>
                                            </tr>  
                                        <tr>
                                            <td width="35%" align="right">新密码：</td>
                                            <td width="65%"><input type="password" name="newPassword"  class="int_text1" value="" size="40" /></td>
                                        </tr> 
                                        <tr>
                                            <td width="35%" align="right">重复密码：</td>
                                            <td width="65%"><input type="password" name="RenewPassword"  class="int_text1" value="" size="40" /></td>
                                        </tr> 
                                        <tr>
                                            <td width="35%" align="right">管理员邮箱：</td>
                                            <td width="65%"><input type="text" name="userEmail"  class="int_text1" value="<?php echo $result['userEmail']; ?>" size="40" /></td>
                                        </tr>
                                        <tr>
                                            <td width="35%" align="right">管理员类型：</td>
                                            <td width="65%">
                                                <select name="userType" <?php echo $userTypeSelect; ?>>
                                                    <option  value="超级管理员" <?php if($result['userType']=='超级管理员'){ echo "selected";} ?>>超级管理员</option> 
                                                    <option  value="普通管理员" <?php if($result['userType']=='普通管理员'){ echo "selected";} ?>>普通管理员</option> 
                                                </select>
                                                 &nbsp;&nbsp;* 超级管理员拥有后台管理的全部权限，普通管理员只有部分权限！
                                            </td>
                                        </tr>  
                                        <tr>
                                            <td width="35%" align="right">性别：</td>
                                            <td width="65%">
                                                <select name="userSex">
                                                    <option  value="男" <?php if($result['userSex']=='男'){ echo "selected";} ?>>男</option> 
                                                    <option  value="女" <?php if($result['userSex']=='女'){ echo "selected";} ?>>女</option> 
                                                    <option value="保密" <?php if($result['userSex']=='保密'){ echo "selected";} ?>>保密</option> 
                                                </select>
                                            </td>
                                        </tr> 
                                        <tr>
                                            <td width="35%" align="right">是否可用：</td>
                                            <td width="65%"><input type="checkbox" name="userEnabled"  <?php if($result['userEnabled']==1){ echo "checked='checked'";} ?> class="int_text1" value="6" size="40" <?php echo $userTypeSelect; ?> /></td>
                                        </tr> 
                                        <tr>
                                            <td width="35%" align="right">备注：</td>
                                            <td width="65%"><input type="text" name="userRemark"  class="int_text1" value="<?php echo $result['userRemark']; ?>" size="40" /></td>
                                        </tr> 
                                        <tr>
                                            <td colspan="2" align="center" height='30'>
                                                <input type="submit" class="btn_ipt01" name="editAdmin" value=" 确定修改 "/>
                                            </td>  
                                        </tr>
                                    </table>
                                </td>
                            </tr>   
                        </table>
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>
